package com.chagee.store.operation.server.service.base;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chagee.store.operation.common.constant.CommonConstant;
import com.chagee.store.operation.server.entity.StoreDO;
import com.chagee.store.operation.server.event.dao.StoreMapper;
import com.chagee.store.operation.server.model.bo.StoreInfoBO;
import com.chagee.store.operation.server.model.param.QueryStoreParam;
import com.chagee.storeManagement.api.event.enums.StoreClosureStatusEnum;

import java.util.Collection;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.stereotype.Service;

/**
 * @ClassName StoreBaseService  //类名称
 * @Description: //类描述
 * @Author: Jacky-zhan	//作者
 * @CreateDate: 2024/04/26 10:25	//创建时间
 * @UpdateUser: Jacky-zhan	//作者
 * @UpdateDate: 2024/04/26 10:25	//更新时间
 * @UpdateRemark: 更新的信息
 * @Version: 1.0    //版本号
 */
@Service
@RequiredArgsConstructor
public class StoreBaseService extends ServiceImpl<StoreMapper, StoreDO> {

	/**
	 * 查询 store
	 *
	 * @param storeNo 门店编号
	 * @return bean
	 */
	public StoreDO queryStoreByStoreNo(String storeNo) {
		QueryWrapper<StoreDO> query = Wrappers.query();
		query.eq("store_no", storeNo).eq("delete_flag", 0);
		query.last("limit 1");
		return baseMapper.selectOne(query);
	}

	/**
	 * 查询开启三方打烊的门店
	 *
	 * @return list
	 */
	public List<StoreDO> selectByClosingTaskStatus(Long id) {
		LambdaQueryChainWrapper<StoreDO> storeQuery = lambdaQuery();
		storeQuery.select(StoreDO::getId, StoreDO::getStoreNo)
			.eq(StoreDO::getClosingTaskStatus, NumberUtils.INTEGER_ONE)
			.eq(StoreDO::getClosureStatus, StoreClosureStatusEnum.OPEN)
			.eq(StoreDO::getDeleteFlag, NumberUtils.INTEGER_ZERO)
			.gt(StoreDO::getId, id)
			.orderByAsc(StoreDO::getId)
			.last("limit 50");
		return baseMapper.selectList(storeQuery.getWrapper());
	}

	public List<StoreInfoBO> queryStoreList(QueryStoreParam param) {
		return baseMapper.queryStoreList(param);
	}

	public List<StoreInfoBO> batchQueryStoreList(List<String> storeNos) {
		return baseMapper.batchQueryStoreList(storeNos);
	}

	public List<StoreDO> queryBaseInfoByStoreNoBatchUpdate(List<String> storeNos) {
		QueryWrapper<StoreDO> wrapper = new QueryWrapper<>();
		wrapper.select("store_no", "store_name", "store_status", "closing_task_status", "id");
		wrapper.in("store_no", storeNos);
		wrapper.eq("delete_flag", NumberUtils.INTEGER_ZERO);
		return baseMapper.selectList(wrapper);
	}

	public StoreDO batchQueryStoreInfo(String storeNo) {
		LambdaQueryWrapper<StoreDO> wrapper = new LambdaQueryWrapper<>();
		wrapper.select(StoreDO::getStoreNo, StoreDO::getStoreName, StoreDO::getStoreCategory, StoreDO::getClosingTaskStatus);
		wrapper.eq(StoreDO::getStoreNo, storeNo);
		wrapper.eq(StoreDO::getDeleteFlag, CommonConstant.NOT_DELETE_FLAG);
		wrapper.last(CommonConstant.LIMIT_ONE_SQL);
		return baseMapper.selectOne(wrapper);
	}

	public List<StoreDO> batchSelectStoreList(Collection<String> storeNos) {
		LambdaQueryWrapper<StoreDO> wrapper = new LambdaQueryWrapper<>();
		wrapper.select(StoreDO::getStoreNo, StoreDO::getStoreCategory, StoreDO::getTag);
		wrapper.in(StoreDO::getStoreNo, storeNos);
		wrapper.eq(StoreDO::getDeleteFlag, NumberUtils.INTEGER_ZERO);
		return baseMapper.selectList(wrapper);
	}

}
